const express = require('express');
const morgan = require('morgan');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3000;

// 创建日志文件
const accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' });

// 使用 morgan 中间件记录日志
app.use(morgan('combined', { stream: accessLogStream }));

// 示例的数据
const users = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Jane' },
  { id: 3, name: 'Doe1' }
];

// 获取所有用户
app.get('/api/users', (req, res) => {
  res.json(users);
});

// 根据 ID 获取特定用户
app.get('/api/users/:id', (req, res) => {
  const user = users.find(u => u.id === parseInt(req.params.id));
  if (!user) return res.status(404).send('User not found');
  res.json(user);
});

app.listen(port, '0.0.0.0', () => {
    console.log('Server is running on port 3000');
});

// app.listen(port, () => {
//   console.log(`API 服务器运行在 http://localhost:${port}`);
// });
